package com.socio.resources;

import java.util.Collections;
import java.util.Random;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import net.sf.jsr107cache.Cache;
import net.sf.jsr107cache.CacheException;
import net.sf.jsr107cache.CacheManager;

import com.google.gson.Gson;
import com.socio.behaviors.DAOBehavior;
import com.socio.behaviors.UserFacebookIDBehavior;
import com.socio.facebook.model.Change;
import com.socio.facebook.model.Entry;
import com.socio.model.Day;
import com.socio.model.User;
import com.socio.utils.SocioUtils;

@Path("/test")
public class TestResource {

	
	
	private static final Gson gson = new Gson();
	
	//@Path("/populate/userDB")
    //@GET
    //@Produces(MediaType.TEXT_PLAIN)
	public String populateDB() {
		
        DAOBehavior userDAO = new UserFacebookIDBehavior();
		
        // 3 top users and then not me
		User user1 = new User();
		user1.setAndroidUDID("000000000000001");
		user1.setFacebookID("1");
		user1.setNickname("Bob");
		user1.setQuickScore(10000);
		User user2 = new User();
		user2.setAndroidUDID("000000000000002");
		user2.setFacebookID("2");
		user2.setNickname("Bill");
		user2.setQuickScore(5000);
		User user3 = new User();
		user3.setAndroidUDID("000000000000003");
		user3.setFacebookID("3");
		user3.setNickname("Big Boss Billy Bob Thorton");
		user3.setQuickScore(3000);
		User user4 = new User();
		user4.setFacebookID("4");
		user4.setAndroidUDID("000000000000004");
		user4.setQuickScore(220);
		
		Day day1 =  new Day();
		day1.setQuickScore(1000);
		Day day2 = new Day();
		day2.setQuickScore(500);
		Day day3 = new Day();
		day3.setQuickScore(220);
		Day day4 = new Day();
		day4.setQuickScore(100);
		
		// 3 top days and another for me today
		long todayTime = SocioUtils.getTodayTime();
		
		day1.setStartTime(todayTime);
		user1.getDays().add(day1);
		userDAO.create(user1);
		user1.getDays().clear();
		day1 = resetDay(day1);
		
		day2.setStartTime(todayTime);
		user2.getDays().add(day2);
		userDAO.create(user2);
		user2.getDays().clear();
		day2 = resetDay(day2);
		
		day3.setStartTime(todayTime);
		user3.getDays().add(day3);
		userDAO.create(user3);
		user3.getDays().clear();
		day3 = resetDay(day3);
		
		day4.setStartTime(todayTime);
		user4.getDays().add(day4);
		userDAO.create(user4);
		user4.getDays().clear();
		day4 = resetDay(day4);
		
		// 3 top days me as number 2 and another day yesterday
		long yesterdayTime = SocioUtils.previousDayFromTime(todayTime);
		
		day1.setStartTime(yesterdayTime);
		user1.getDays().add(day1);
		userDAO.create(user1);
		user1.getDays().clear();
		day1 = resetDay(day1);
		
		day3.setStartTime(yesterdayTime);
		user2.getDays().add(day3);
		userDAO.create(user2);
		user2.getDays().clear();
		day3 = resetDay(day3);
		
		day4.setStartTime(yesterdayTime);
		user3.getDays().add(day4);
		userDAO.create(user3);
		user3.getDays().clear();
		day4 = resetDay(day4);
		
		day2.setStartTime(yesterdayTime);
		user4.getDays().add(day2);
		userDAO.create(user4);
		user4.getDays().clear();
		day2 = resetDay(day2);
		
		// 1 day which is me 2 days ago
		long twoDaysAgoTime = SocioUtils.previousDayFromTime(yesterdayTime);
		
		day1.setStartTime(twoDaysAgoTime);
		user4.getDays().add(day1);
		userDAO.create(user4);
		user4.getDays().clear();
		day1 = resetDay(day1);
		
		// 3 top days and me with 0 pts 3 days ago
		long threeDaysAgoTime = SocioUtils.previousDayFromTime(twoDaysAgoTime);
		
		day1.setStartTime(threeDaysAgoTime);
		user1.getDays().add(day1);
		userDAO.create(user1);
		user1.getDays().clear();
		day1 = resetDay(day1);
		
		day2.setStartTime(threeDaysAgoTime);
		user2.getDays().add(day2);
		userDAO.create(user2);
		user2.getDays().clear();
		day2 = resetDay(day2);
		
		day3.setStartTime(threeDaysAgoTime);
		user3.getDays().add(day3);
		userDAO.create(user3);
		user3.getDays().clear();
		day3 = resetDay(day3);
		
		return "userDB populated";
	}
    
	//@Path("/simulate/changes")
	//@GET
    //@Produces(MediaType.TEXT_PLAIN)
    public String simulateChanges() {
    	FacebookResource resource = new FacebookResource();
		Change change = new Change();
		Random random = new Random(System.currentTimeMillis());
		for(int i=0; i<10; i++) {
			Entry entry = new Entry();
			entry.getChangedFields().add("Email");
			
			Float randomUid = random.nextFloat()*3.99f+1;
			entry.setUid(randomUid.longValue());
			change.getEntry().add(entry);
		}
		resource.getChanges(gson.toJson(change));
		
		return "changes simulated";
	}
	
    @Path("/clearCache")
    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String clearCache() {
		
        Cache cache = null;
        try {
	        cache = CacheManager.getInstance().getCacheFactory().createCache(Collections.emptyMap());
        } catch (CacheException e) {}
        cache.clear();
        
        return "cache cleared";
	}
	
	public Day resetDay(Day day){
		day.setUser(null);
		day.setKey(null);
		
		return day;
	}	
}
